package com.kaishengit.web;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.entity.Employee;
import com.kaishengit.service.EmployeeService;

public class LoginServlet extends HttpServlet {


	private static final long serialVersionUID = 1L;
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		EmployeeService service = new EmployeeService();
		Employee employee = service.findByName(username);
		if(employee != null && employee.getPassword().equals(password)){
			HttpSession session = request.getSession();
			session.setAttribute("cur_user", employee);
			
			response.sendRedirect("project.jspx");
	}else{
		
		response.sendRedirect("login.jspx?code=1001");
		}
	}
	
	
}

